package me.bzcoder.easywebview.base

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.os.Build
import android.os.Process
import android.util.AttributeSet
import android.view.View
import com.tencent.smtt.sdk.QbSdk
import com.tencent.smtt.sdk.WebSettings
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient

/**
 *
 * X5WebView基础类
 *
 * @author : BaoZhou
 * @date : 2019/7/19 15:25
 */
open class BaseX5WebView : WebView {
    var IS_LOG = false
    private val client: WebViewClient = object : WebViewClient() {
        /**
         * 防止加载网页时调起系统浏览器
         */
        override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
            view.loadUrl(url)
            return true
        }
    }

    constructor(arg0: Context?) : super(arg0) {
        setBackgroundColor(85621)
    }

    @SuppressLint("SetJavaScriptEnabled")
    constructor(arg0: Context?, arg1: AttributeSet?) : super(arg0, arg1) {
        this.webViewClient = client
        // this.setWebChromeClient(chromeClient);
        // WebStorage webStorage = WebStorage.getInstance();
        initWebViewSettings()
        this.view.isClickable = true
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    private fun initWebViewSettings() {
        val webSetting = this.settings
        webSetting.javaScriptEnabled = true
        webSetting.javaScriptCanOpenWindowsAutomatically = true
        webSetting.allowFileAccess = true
        webSetting.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        webSetting.setSupportZoom(true)
        webSetting.builtInZoomControls = true
        webSetting.useWideViewPort = true
        webSetting.setSupportMultipleWindows(true)
        // webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true)
        // webSetting.setDatabaseEnabled(true);
        webSetting.domStorageEnabled = true
        webSetting.setGeolocationEnabled(true)
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE)
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.pluginState = WebSettings.PluginState.ON_DEMAND
        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSetting.cacheMode = WebSettings.LOAD_NO_CACHE
        // webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。MIXED_CONTENT_ALWAYS_ALLOW
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSetting.mixedContentMode = WebSettings.LOAD_NORMAL
        }

        // this.getSettingsExtension().setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);//extension
        // settings 的设计
    }

    override fun drawChild(canvas: Canvas, child: View, drawingTime: Long): Boolean {
        val ret = super.drawChild(canvas, child, drawingTime)
        if (IS_LOG) {
            canvas.save()
            val paint = Paint()
            paint.color = 0x7fff0000
            paint.textSize = 24f
            paint.isAntiAlias = true
            if (x5WebViewExtension != null) {
                canvas.drawText(
                    this.context.packageName + "-pid:"
                            + Process.myPid(), 10f, 50f, paint
                )
                canvas.drawText(
                    "X5  Core:" + QbSdk.getTbsVersion(this.context), 10f, 100f, paint
                )
            } else {
                canvas.drawText(
                    this.context.packageName + "-pid:"
                            + Process.myPid(), 10f, 50f, paint
                )
                canvas.drawText("Sys Core", 10f, 100f, paint)
            }
            canvas.drawText(Build.MANUFACTURER, 10f, 150f, paint)
            canvas.drawText(Build.MODEL, 10f, 200f, paint)
            canvas.restore()
        }
        return ret
    }

    /**
     * 打开后开启监测
     */
    fun openLog() {
        IS_LOG = true
    }
}